    /****************************************************************************

 Copyright (C) 2002-2007 Gilles Debunne (Gilles.Debunne@imag.fr)

 This file is part of the QGLViewer library.
 Version 2.3.0, released on June 29, 2008.

 http://artis.imag.fr/Members/Gilles.Debunne/QGLViewer

 libQGLViewer is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 2 of the License, or
 (at your option) any later version.

 libQGLViewer is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with libQGLViewer; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

*****************************************************************************/

#define QT_VERSION  0x040000
#include <QGLViewer/qglviewer.h>
#include "consoleWidget.h"

// A class used to load images for OpenGL using SDL
//#include <guichan/opengl/openglsdlimageloader.hpp>
//#include "keyListener.h"
#include "consoleWidget.h"
#include "baijiAccessor.h"

#include "sceneManager.h"
#include "meshManager.h"
#include "meshType.h"
#include "tetgenParser.h"
#include "objParser.h"

#include <QTextEdit>
#include <QDockWidget>
#include <QKeyEvent>
#include <QMouseEvent>
#include <QMessageBox>


using namespace MeshUtils;

typedef void ( *ptToSetMat ) ( void* );


class Viewer : public QGLViewer
{
        GLuint index;
        GLubyte lists;
        bool update;

    public:
        //////////////////////
        //  mesh Management //
        //////////////////////
        void addMesh(Mesh& msh, string name);
        Entity& createEntity(string name, string mshName, string matName);
        void linkEntityToRoot(string name);
        SceneNode&  getRoot();
        void addMaterial(const Material m, string name);

        /////////////////////
		//Drawing functions//
		/////////////////////
		static void setMat ( void* );

		Viewer() : QGLViewer(), drawTets(false), update(true) {};



		bool drawTets;

    protected :
			/////////////////////
			//Drawing functions//
			/////////////////////
			void drawTriangles(ptToSetMat setMatp, void* matArg, std::vector<Vector3>& nodes, std::vector<Face_t> faces);
			void drawShrinkedTriangles(ptToSetMat setMatp, std::vector<Triangle>& tList, const double shrinkFactor);
			void drawShrinkedTriangles(ptToSetMat setMatp, std::vector<Vector3>& nodes, std::vector<Face_t> faces, const double shrinkFactor);
			void drawTetrahedra(ptToSetMat setMatp, void* matArg, Mesh& mesh, const double shrinkFactor);
			void drawScene ( ptToSetMat setMatp );
			void drawRec(SceneNode& root, SceneManager& scnMng, MeshManager& mshMng);
			virtual void draw();

			/////////////////////
			//Picking functions//
			/////////////////////
			void drawWithNames();
			void drawRecInSelectionMode(SceneNode& root, SceneManager& scnMng, MeshManager& mshMng);
			void postSelection(const QPoint& point);



            //////////////////////////
			// Interaction functions//
			//////////////////////////
			void select(const QPoint & point);
            virtual void keyPressEvent(QKeyEvent *e);
            virtual QString helpString() const;


           virtual void init();


			/////////////////////
			//protected members//
			/////////////////////
			Console* console;
            MeshManager mshMng;
            SceneManager scnMng;
};

struct MatInfo
{
    Viewer* This;
    const char* name;
};
